using System;
using System.Collections.Generic;
using System.Text;
using OpenComposite.Base.Designer;
using System.Drawing;
using OpenComposite.EII.Designer.BRules;

namespace OpenComposite.EII.Designer.Generic
{
	public class ANDSymbol : Symbol, IConnector, IConnectorFactory
	{
		#region Initialization
		public ANDSymbol()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			//this.Name = "andSymbol" + GetCount(typeof(ANDSymbol));
			this.Text = "AND";

			this.Border = BorderType.Diamond;
			this.BorderColor = Color.Blue;
			this.BorderWidth = 3;

			this.NameTextBox.Hide();

			this.Size = new Size(40, 40);
			this.MinimumSize = new Size(40, 40);
			this.MaximumSize = new Size(40, 40);

			this.EndUpdate();
		}
		#endregion

		#region Public Members

		#region Override

		#region Properties
		public override bool CanFocused
		{
			get
			{
				return true;
			}
		}
		public override bool CanMove
		{
			get
			{
				return true;
			}
		}
		public override bool CanResize
		{
			get
			{
				return false;
			}
		}
		public override bool CanSelect
		{
			get
			{
				return true;
			}
		}
		public override bool IsContainer
		{
			get
			{
				return false;
			}
		}
		public override bool NoClip
		{
			get
			{
				return false;
			}
		}
		public override SizeF GridOffset
		{
			get {
				if ( this.DesignerViewControl != null ) {
					Size sz = this.DesignerViewControl.GridSize;
					return new SizeF(sz.Width / 2f, sz.Height / 2f);
				} else
					return base.GridOffset;
			}
		}
		#endregion

		#region Drag & Drop
		public override object GetDragObject(Point location, System.Windows.Forms.MouseButtons button)
		{
			return this;
		}
		public override bool IsDropAllowed(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			return ( ( obj is IFSymbol || obj is ANDSymbol || obj is ORSymbol ||
					   obj is BusinessProcess.ActivitySymbol || obj is BusinessProcess.WhileSymbol ) && obj != this &&
				button == System.Windows.Forms.MouseButtons.Right );
		}
		public override void DropObject(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			if ( this.IsDropAllowed(obj, location, button) )
				this.DesignerViewControl.AddRoute((DesignerControl)obj, this);
		}
		#endregion

		#region Paint
		public override void PaintBackground(System.Windows.Forms.PaintEventArgs pe)
		{
			base.PaintBackground(pe);
			Rectangle bounds = this.GetPaintBounds();
			//bounds.Inflate(-10, -10);
			//int x1 = bounds.Left;
			//int x2 = bounds.Right;
			//int x3 = x1 + (int)( ( x2 - x1 ) / 2 );
			//int y1 = bounds.Top;
			//int y2 = bounds.Bottom;
			//int y3 = y1 + (int)( ( y2 - y1 ) / 2 );
			//pe.Graphics.DrawLine(this.BorderPen, x1, y3, x2, y3); // draw horizontal line
			//pe.Graphics.DrawLine(this.BorderPen, x3, y1, x3, y2); // draw vertical line
			using ( StringFormat sf = new StringFormat(StringFormat.GenericTypographic)) {
				sf.Alignment = StringAlignment.Center;
				sf.LineAlignment = StringAlignment.Center;
				pe.Graphics.DrawString("AND", this.Font, this.ForeBrush, bounds, sf);
			}
		}
		public override void Paint(System.Windows.Forms.PaintEventArgs pe)
		{
			base.Paint(pe);
		}
		#endregion

		#endregion

		#endregion

		#region Protected Members

		#region Override
		protected override void OnVisibleChanged()
		{
			// do nothing
		}
		#endregion

		#endregion

		#region IConnector Members

		public Point GetRouteEnd(RouteDockDirection dock)
		{
			return getRoutePoint(dock);
		}

		public Point GetRouteStart(RouteDockDirection dock)
		{
			return getRoutePoint(dock);
		}
		private Point getRoutePoint(RouteDockDirection dock)
		{
			Point p;
			Rectangle bounds = this.GetPaintBounds();
			int x1 = bounds.Left;
			int x2 = bounds.Right;
			int x3 = x1 + (int)( ( x2 - x1 ) / 2 );
			int y1 = bounds.Top;
			int y2 = bounds.Bottom;
			int y3 = y1 + (int)( ( y2 - y1 ) / 2 );
			switch ( dock ) {
				case RouteDockDirection.Bottom:
					p = new Point(x3, y2);
					break;
				case RouteDockDirection.Left:
					p = new Point(x1, y3);
					break;
				case RouteDockDirection.Right:
					p = new Point(x2, y3);
					break;
				case RouteDockDirection.Top:
					p = new Point(x3, y1);
					break;
				default:
					p = new Point(x1, y3);
					break;
			}
			return p;
		}

		#endregion

		#region IConnectorFactory Members

		public override bool CreateRouteEnd(DesignerControl controlStart,
			out IConnector connector, out System.Drawing.Drawing2D.LineCap cap,
			out System.Drawing.Drawing2D.CustomLineCap customcap, out RouteDockDirection dockStart,
			out RouteDockDirection dockEnd, out bool useEndConnectorAsRouteEnd)
		{
			return base.CreateRouteEnd(controlStart,
				out connector, out cap, out customcap, out dockStart, out dockEnd, out useEndConnectorAsRouteEnd);
		}

		public override bool CreateRouteStart(DesignerControl controlEnd,
			out IConnector connector, out System.Drawing.Drawing2D.LineCap cap,
			out System.Drawing.Drawing2D.CustomLineCap customcap, out RouteDockDirection dockStart,
			out RouteDockDirection dockEnd, out bool useStartConnectorAsRouteStart)
		{
			return base.CreateRouteStart(controlEnd,
				out connector, out cap, out customcap, out dockStart, out dockEnd, out useStartConnectorAsRouteStart);
		}

		#endregion
	}
}
